package com.wd.live.view.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.wd.live.R;
import com.wd.live.base.BaseFragment;
import com.wd.live.bean.LiveTypeListBean;
import com.wd.live.databinding.SquareFragmentBinding;
import com.wd.live.bean.FindLiveListBean;
import com.wd.live.model.SpUtils;
import com.wd.live.view.activity.ConstellationActivity;
import com.wd.live.view.activity.LookLiveActivity;
import com.wd.live.viewmodel.MainViewModel;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>项目名称:LookFor2.0</p>
 * <p>包名:com.bawei.lookfor20.view.fragment</p>
 * <p>简述:</p>
 *
 * @author 孟令高
 * @date 2022/11/9
 */
public class SquareFragment extends BaseFragment<MainViewModel, SquareFragmentBinding> {
    private Integer usrid;
    private int page=1;
    private int index=100;
    private ArrayList<FindLiveListBean.ResultBean> list;
    private int size=10;
    private List<String> sqqlists = null;
    private List<LiveTypeListBean.ResultBean> liveTypeListResult;
    @Override
    protected void initData() {
        String uid = SpUtils.getString("uid", "");
        usrid = Integer.valueOf(uid);
        sqqlists = new ArrayList<>();
        if(databind.squTab.getTabCount()==0){
            viewmodel.getlivetypelist().observe(getActivity(), new Observer<LiveTypeListBean>() {
                @Override
                public void onChanged(LiveTypeListBean liveTypeListBean) {
                    liveTypeListResult = liveTypeListBean.getResult();
                    for (LiveTypeListBean.ResultBean resultBean : liveTypeListResult) {
                        sqqlists.add(resultBean.getName());
                    }
                    sqqlists.add(0, "关注");
                    sqqlists.add(4, "跳舞");
                    for (String str : sqqlists) {
                        databind.squTab.addTab(databind.squTab.newTab().setText(str));
                    }
                    for (int i = 0;i < sqqlists.size();i++){
                        TabLayout.Tab tab = databind.squTab.getTabAt(i);
                        if (tab != null){
                            tab.setCustomView(Tabs(i));
                        }
                    }
                    updateTabTextView(databind.squTab.getTabAt(1),true);
                    databind.squTab.getTabAt(1).select();
                    //imgtab监听
                    TabLinearLayout();

                }
            });
        }

        getfindVideolist(usrid,index,page,size);
        RefreshLoadMore();





        databind.xingzuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ConstellationActivity.class));
            }
        });
    }

    private void RefreshLoadMore() {
        databind.squareSma.autoRefresh();
        databind.squareSma.autoLoadMore();
        databind.squareSma.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getfindVideolist(usrid,index,page,size);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                list.clear();
                getfindVideolist(usrid,index,page,size);
            }
        });
    }

    //获取直播列表
    private void getfindVideolist(int userid, int livetype, int page, int size) {
        viewmodel.getlivelist(userid,livetype,page,size).observe(getActivity(), new Observer<FindLiveListBean>() {
            @Override
            public void onChanged(FindLiveListBean findLiveListBean) {
                databind.squareSma.finishLoadMore();
                databind.squareSma.finishRefresh();
                List<FindLiveListBean.ResultBean> result = findLiveListBean.getResult();
                list = new ArrayList<>();
                list.addAll(result);
                if (result == null || !(result.size()>0)){
                    Toast.makeText(getActivity(), "已无法加载更多", Toast.LENGTH_SHORT).show();
                }else{
                    if ((list.size()>0)){
                        list.addAll(result);
                        initSquareAdapter();
                    }else{
                        list.addAll(result);
                        databind.squareRec.getAdapter().notifyDataSetChanged();
                    }
                }

            }

        });
    }
    private View Tabs(int currentPosition) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.tab_text);
        textView.setText(sqqlists.get(currentPosition));
        return view;
    }
    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_text);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextSize(18);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_text);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextSize(16);
            tabUnSelect.setText(tab.getText());
        }
    }
    private void initSquareAdapter() {
        //查看直播列表
        databind.squareRec.setLayoutManager(new GridLayoutManager(getContext(), 2));
        databind.squareRec.setAdapter(new CommonAdapter<FindLiveListBean.ResultBean>(getContext(), R.layout.find_live_list, list) {
            @SuppressLint("CheckResult")
            @Override
            protected void convert(ViewHolder holder, FindLiveListBean.ResultBean resultBean, int position) {
                if (resultBean.getLiveStatus() == 1) {//判断是否在直播
                    holder.setText(R.id.rec_text, "直播中");
                    holder.getView(R.id.rec_rl).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.rec_rl).setVisibility(View.GONE);
                }
                //图片圆角
                RoundedCorners roundedCorners = new RoundedCorners(16);
                //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
                // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                Glide.with(holder.itemView.getContext()).load(resultBean.getLiveCoverUrl())
                        .apply(options)
                        .error(R.mipmap.zhanweitu)
                        .placeholder(R.mipmap.zhanweitu)
                        .into((ImageView) holder.getView(R.id.rec_img));//直播封面
                holder.setText(R.id.rec_text_name, resultBean.getNickName());//主播名称
                holder.setText(R.id.rec_text_personality_name, resultBean.getLiveTitle());//直播标题
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), LookLiveActivity.class);
                        intent.putExtra("liveid",resultBean.getLiveId());
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    protected int initLayout() {
        return R.layout.square_fragment;
    }

    @Override
    public void onChanged(Object o) {

    }
    private void TabLinearLayout() {
        databind.squTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        index = 100;
                        page = 1;
                        list.clear();
                        updateTabTextView(databind.squTab.getTabAt(0),true);
                        updateTabTextView(databind.squTab.getTabAt(1),false);
                        updateTabTextView(databind.squTab.getTabAt(2),false);
                        updateTabTextView(databind.squTab.getTabAt(3),false);
                        updateTabTextView(databind.squTab.getTabAt(4),false);
                        updateTabTextView(databind.squTab.getTabAt(5),false);
                        //查询列表
                        getfindVideolist(usrid, index, page, size);
                        break;
                    case 4:
                        index = 101;
                        page = 1;
                        list.clear();
                        updateTabTextView(databind.squTab.getTabAt(0),false);
                        updateTabTextView(databind.squTab.getTabAt(1),false);
                        updateTabTextView(databind.squTab.getTabAt(2),false);
                        updateTabTextView(databind.squTab.getTabAt(3),false);
                        updateTabTextView(databind.squTab.getTabAt(4),true);
                        updateTabTextView(databind.squTab.getTabAt(5),false);
                        getfindVideolist(usrid, index, page, size);
                        break;
                    case 5:
                        index = 104;
                        page = 1;
                        list.clear();
                        updateTabTextView(databind.squTab.getTabAt(0),false);
                        updateTabTextView(databind.squTab.getTabAt(1),false);
                        updateTabTextView(databind.squTab.getTabAt(2),false);
                        updateTabTextView(databind.squTab.getTabAt(3),false);
                        updateTabTextView(databind.squTab.getTabAt(4),false);
                        updateTabTextView(databind.squTab.getTabAt(5),true);
                        getfindVideolist(usrid, index, page, size);
                        break;
                    default:
                        index = liveTypeListResult.get(tab.getPosition()).getId();
                        page = 1;
                        list.clear();
                        switch (tab.getPosition()){
                            case 1:
                                updateTabTextView(databind.squTab.getTabAt(0),false);
                                updateTabTextView(databind.squTab.getTabAt(1),true);
                                updateTabTextView(databind.squTab.getTabAt(2),false);
                                updateTabTextView(databind.squTab.getTabAt(3),false);
                                updateTabTextView(databind.squTab.getTabAt(4),false);
                                updateTabTextView(databind.squTab.getTabAt(5),false);
                                break;
                            case 2:
                                updateTabTextView(databind.squTab.getTabAt(0),false);
                                updateTabTextView(databind.squTab.getTabAt(1),false);
                                updateTabTextView(databind.squTab.getTabAt(2),true);
                                updateTabTextView(databind.squTab.getTabAt(3),false);
                                updateTabTextView(databind.squTab.getTabAt(4),false);
                                updateTabTextView(databind.squTab.getTabAt(5),false);
                                break;
                            case 3:
                                updateTabTextView(databind.squTab.getTabAt(0),false);
                                updateTabTextView(databind.squTab.getTabAt(1),false);
                                updateTabTextView(databind.squTab.getTabAt(2),false);
                                updateTabTextView(databind.squTab.getTabAt(3),true);
                                updateTabTextView(databind.squTab.getTabAt(4),false);
                                updateTabTextView(databind.squTab.getTabAt(5),false);
                                break;
                        }
                        getfindVideolist(usrid, index, page, size);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}